package top.xana.acg.anime.ui.page

import android.graphics.Rect
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.lifecycle.LifecycleOwner
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.vlayout.DelegateAdapter
import com.alibaba.android.vlayout.VirtualLayoutManager
import com.alibaba.android.vlayout.layout.GridLayoutHelper
import com.alibaba.android.vlayout.layout.LinearLayoutHelper
import com.youth.banner.Banner
import com.youth.banner.adapter.BannerAdapter
import com.youth.banner.indicator.CircleIndicator
import top.xana.acg.anime.R
import top.xana.acg.anime.model.Anime
import top.xana.acg.anime.model.HomeBanner
import top.xana.acg.anime.model.Label
import top.xana.acg.common.base.adp.BaseDelegateAdapter
import top.xana.acg.common.base.adp.BaseViewHolder
import top.xana.acg.common.base.adp.RecyclerAdapter
import top.xana.acg.common.base.ui.BaseFragment
import top.xana.acg.common.utils.*

class HomeFragment : BaseFragment() {

    companion object {

        const val TAG = "HomeFragment"
        fun newInstance() = HomeFragment()
    }

    private lateinit var viewModel: HomeViewModel
    override fun getLayoutId() = R.layout.home_fragment

    override fun initView() {
        super.initView()

        initHomeView()


    }

    private fun initHomeView() {
        val lp = VirtualLayoutManager(requireContext())
        val adp = DelegateAdapter(lp)
        getView<RecyclerView>(R.id.homeView).let {
            it.layoutManager = lp
            it.adapter = adp
        }

        adp.addAdapterSeq(
            HomeBannerAdapter(
                listOf(
                    HomeBanner(""),
                    HomeBanner(""),
                    HomeBanner(""),
                    HomeBanner(""),
                    HomeBanner(""),
                    HomeBanner("")
                ), this
            ),
            LabAdapter(Label("我的追番", "")),
            FavorAdapter(List(10) { Anime() }),
            LabAdapter(Label("正在热播", "")),
            HotAdapter(
                List(6) { Anime() },
                requireView().dp2px(R.dimen.dp_15),
                requireView().dp2px(R.dimen.dp_10),
                requireView().dp2px(R.dimen.dp_15)
            ),
            LabAdapter(Label("新番时间表", ""))
        // TODO jre
        )

        val arr = resources.getStringArray(R.array.mainTab)
        arr.forEachIndexed { index, s ->
            if (index > 0 && index < arr.size - 1) {
                adp.addAdapterSeq(
                    LabAdapter(Label(s, "")),
                    CategoryAdapter(
                        List(12) { Anime() },
                        requireView().dp2px(R.dimen.dp_15),
                        requireView().dp2px(R.dimen.dp_7),
                        requireView().dp2px(R.dimen.dp_10)
                    )
                )
            }
        }
    }

    class HomeBannerAdapter(val banners: List<HomeBanner>, val lifecycleOwner: LifecycleOwner) :
        BaseDelegateAdapter(LinearLayoutHelper(), R.layout.vlayout_banner, 1) {
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder {
            val holder = super.onCreateViewHolder(parent, viewType)
            holder.getView<Banner<HomeBanner, BannerAdp>>(R.id.home_banner).apply {
                radius(R.dimen.dp_8)
                adapter = BannerAdp(banners)
                addBannerLifecycleObserver(lifecycleOwner)
                indicator = CircleIndicator(context)
                addItemDecoration(object : RecyclerView.ItemDecoration() {
                    override fun getItemOffsets(
                        outRect: Rect,
                        view: View,
                        parent: RecyclerView,
                        state: RecyclerView.State
                    ) {
//                        if(parent.getChildAdapterPosition(view)< banners.size-1)
//                            outRect.right = dp2px(R.dimen.dp_10)
                    }
                })
            }
            return holder
        }
    }

    class BannerAdp(banners: List<HomeBanner>) :
        BannerAdapter<HomeBanner, BaseViewHolder>(banners) {
        override fun onCreateHolder(parent: ViewGroup?, viewType: Int): BaseViewHolder {
            val view = LayoutInflater.from(parent!!.context).inflate(
                R.layout.item_home_banner, parent,
                false
            )
            view.radius(R.dimen.dp_8)
            return BaseViewHolder(view)
        }

        override fun onBindView(
            holder: BaseViewHolder,
            data: HomeBanner?,
            position: Int,
            size: Int
        ) {
            holder.getView<ImageView>(R.id.iv_banner).setDrawable(R.drawable.yukinoa)
        }

    }

    class LabAdapter(val lab: Label) :
        BaseDelegateAdapter(LinearLayoutHelper(), R.layout.vlayout_label_bar, 1) {
        override fun onBindViewHolder(holder: BaseViewHolder, position: Int) {
            super.onBindViewHolder(holder, position)
            holder.getView<TextView>(R.id.tv_label).text = lab.labName
            holder.getView<TextView>(R.id.btn_label).apply {
            }
        }
    }


    class FavorAdapter(val animes: List<Anime>) :
        BaseDelegateAdapter(LinearLayoutHelper(), R.layout.vlayout_recyclerview, 1) {
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder {
            val holder = super.onCreateViewHolder(parent, viewType)
            holder.getView<RecyclerView>(R.id.recyclerview_hor).run {
                layoutManager = LinearLayoutManager(context, RecyclerView.HORIZONTAL, false)
                adapter = object : RecyclerAdapter<Anime>(animes.toMutableList(), null) {
                    override fun getItemViewType(position: Int, data: Anime) =
                        R.layout.item_favor_view

                    override fun onCreateViewHolder(view: View, viewType: Int) =
                        object : RecyclerAdapter.ViewHolder<Anime>(view) {
                            override fun onBind(data: Anime) {
                                // TODO
                            }
                        }
                }
                addItemDecoration(object : RecyclerView.ItemDecoration() {
                    override fun getItemOffsets(
                        outRect: Rect,
                        view: View,
                        parent: RecyclerView,
                        state: RecyclerView.State
                    ) {
                        val i = parent.getChildAdapterPosition(view)
                        if (0 == i) {
                            outRect.left = dp2px(R.dimen.dp_15)
                        } else if (animes.size - 1 == i) {
                            outRect.left = dp2px(R.dimen.dp_10)
                            outRect.right = dp2px(R.dimen.dp_15)
                        } else {
                            outRect.left = dp2px(R.dimen.dp_10)
                        }

                    }
                })
            }
            return holder
        }
    }

    class HotAdapter(val animes: List<Anime>, vGap: Int, hGap: Int, margin: Int) :
        BaseDelegateAdapter(GridLayoutHelper(2, 6, vGap, hGap).also {
            it.setMargin(
                margin,
                margin,
                margin,
                margin
            )
        }, R.layout.vlayout_hot_view, 6) {
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder {
            val holder = super.onCreateViewHolder(parent, viewType)

            return holder
        }
    }


    class CategoryAdapter(val animes: List<Anime>, vGap: Int, hGap: Int, margin: Int) :
        BaseDelegateAdapter(GridLayoutHelper(3, 6, vGap, hGap).also {
            it.setMargin(
                margin,
                margin,
                margin,
                margin
            )
        }, R.layout.vlayout_category_view, 12) {
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder {
            val holder = super.onCreateViewHolder(parent, viewType)
            return holder
        }
    }
}